package view.diaLogView;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.text.R;

import java.io.File;

/**
 * Created by Administrator on 2016/6/21.
 *图片墙的dialog
 */
public class ChangHeadDialog extends Dialog {
    private final int LOCAL = 4;//相册
    private final int CAMERA = 5;//相机
    public static final String IMAGE_FILE_NAME= "Photo_wall.jpg";
    public  Context mContext;
    private ChangHeadDialog mChangHeadDialog;
    private OnCancelClickListener mOnCancelClickListener;
    public ChangHeadDialog(Context context) {
        super(context, R.style.lable_del_dialog);
        mChangHeadDialog = this;
        this.mContext = context;
    }

    @Override public void show() {
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.share_style);
        super.show();
    }
    public ChangHeadDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected ChangHeadDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.width = mContext.getResources().getDisplayMetrics().widthPixels;
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View mView = inflater.inflate(R.layout.weaccount_head_dialog, null);
        LinearLayout linearLayout = (LinearLayout) mView.findViewById(R.id.linea_dialog);
        TextView album = (TextView)mView.findViewById(R.id.tv_weaccountheaddlg_album);//相册
        TextView cancel= (TextView)mView.findViewById(R.id.tv_weaccountheaddlg_cancel);//取消
        TextView photograph = (TextView)mView.findViewById(R.id.tv_weaccountheaddlg_photograph);//拍照

        album.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                Intent intentFromGallery;
                if (android.os.Build.VERSION.SDK_INT >= 19) { // 判断是不是4.4
                    intentFromGallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                } else {
                    intentFromGallery = new Intent(Intent.ACTION_GET_CONTENT);
                }
                intentFromGallery.setType("image/*"); // 设置文件类型
                ((Activity)mContext).startActivityForResult(intentFromGallery,LOCAL);
                mChangHeadDialog.dismiss();
                return;
            }
        });

        photograph.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getFile()));
                ((Activity)mContext).startActivityForResult(intent,CAMERA);
                mChangHeadDialog.dismiss();
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                mChangHeadDialog.dismiss();
                mOnCancelClickListener.onItemClick();
            }
        });

        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                mChangHeadDialog.dismiss();
                mOnCancelClickListener.onItemClick();
            }
        });
        setContentView(mView, params);
    }

    /**
     * 获取file的时候如果没有路径就重新创建
     * @return
     */
    private File getFile() {
        File file = new File(Environment.getExternalStorageDirectory(), IMAGE_FILE_NAME);
        if (!file.exists()) {
            file.getParentFile().mkdirs();
        }
        return file;
    }

    /**
     * 定义接口 用于取消事件的回调
     */
    public interface OnCancelClickListener {
        void onItemClick();
    }
    public void setOnItemClickListener(OnCancelClickListener listener) {
        this.mOnCancelClickListener = listener;
    }
}
